<?php

class PasteLogModel extends MVC_Model
{
    function PasteLogModel()
    {
        parent::MVC_Model();
    }

    function getRecents($limit = 10)
    {
        return $this->getAll("SELECT id, nickname, post_time FROM pasteLog ORDER BY post_time DESC LIMIT ?", array($limit));
    }

    function getPaste($id)
    {
        return $this->getRow("SELECT id, post_time, nickname, lang, code FROM pasteLog WHERE id = ?", array($id));
    }

    /**
     * Save on database
     *
     * The structure of $data variable is:
     *
     * array( nickname, code language, code);
     * 
     * @param array $data 
     * @access public
     * @return void
     */
    function savePaste($data)
    {
        $sql = "INSERT INTO pasteLog (post_time, nickname, lang, code, longip) VALUES (NOW(), ?, ?, ?, ?)";
        
        $rs = $this->query($sql, $data);

        return $this->getOne('SELECT LAST_INSERT_ID()');
    }

    function countPastesByIP($longip)
    {
        $t = $this->getOne(
            'SELECT COUNT(*) FROM pasteLog
            WHERE longip = ? AND post_time > NOW() - INTERVAL 3 MINUTE',
            array($longip));

        return $t;
    }

    function autoManagement()
    {
        $rs = $this->query('DELETE FROM pasteLog WHERE post_time < (NOW() - INTERVAL 90 DAY)');
        if (DB::isError($rs)) {
            die($rs->toString());
        }
    }
}

?>
